package com.ruoyi.ruoyimaintenance.mapper;

import com.ruoyi.ruoyimaintenance.domain.entity.MaintenanceDL;
import com.ruoyi.ruoyimaintenance.domain.entity.MaintenanceDeviceConfig;
import com.ruoyi.ruoyimaintenance.domain.entity.MaintenanceLogdata;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MaintenanceLogdataMapper {


    @Results(id="MaintenanceDeviceConfigMap", value = {
            @Result(property = "id", column = "id", id = true), // 通常id字
            @Result(property = "maintenanceDeviceConfig", column = "device_config_id", javaType = MaintenanceDeviceConfig.class,
                    one = @One(select = "com.ruoyi.ruoyimaintenance.mapper.MaintenanceDeviceConfigMapper.selectMaintenanceDeviceConfigById")),
            @Result(property = "maintenanceDate", column = "maintenance_date"),
            @Result(property = "machineHead", column = "machine_head"),
            @Result(property = "headCount", column = "head_count"),
            @Result(property = "currentUsageDays", column = "current_usage_days"),
            @Result(property = "maintenanceItem", column = "maintenance_item"),
            @Result(property = "maintenanceResult", column = "maintenance_result"),
            @Result(property = "abnormalDescription", column = "abnormal_description"),
    })
    @Select({"<script>",
            "select ml.* from maintenance_logdata as ml",
            "INNER JOIN maintenance_device_config as mdc on ml.device_config_id = mdc.id",
            "<where>",
            "   <if test='line != null'>AND mdc.line = #{line}</if>",
            "   <if test='machineNumber != null and machineNumber != \"\"'>AND mdc.machine_number LIKE CONCAT('%', #{machineNumber}, '%')</if>",
            "   <if test='headNumber != null and headNumber != \"\"'>AND mdc.head_number LIKE CONCAT('%', #{headNumber}, '%')</if>",
            "   <!-- 时间条件：有参数则用参数，无参数则查当月 -->",
            "   <choose>",
            "       <when test='timeStar != null and timeStar != \"\" and timeEnd != null and timeEnd != \"\"'>",
            "           AND ml.maintenance_date BETWEEN #{timeStar} AND #{timeEnd}",
            "       </when>",
            "       <otherwise>",
            "           AND ml.maintenance_date BETWEEN DATE_FORMAT(CURDATE(), '%Y-%m-01') AND LAST_DAY(CURDATE())",
            "       </otherwise>",
            "   </choose>",
            "</where>",
            "</script>"})
    List<MaintenanceLogdata> maintenanceLogdataBySearch(@Param("line")Integer line,@Param("machineNumber")String machineNumber,@Param("headNumber")String headNumber,@Param("timeStar")String timeStar,@Param("timeEnd")String timeEnd);

    @Results(id = "maintenanceRecordMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "line", column = "line"),
            @Result(property = "side", column = "side"),
            @Result(property = "maintenanceDate", column = "maintenance_date"),
            @Result(property = "deviceConfigId", column = "device_config_id"),
            @Result(property = "machineNumber", column = "machine_number"),
            @Result(property = "machineHead", column = "machine_head"),
            @Result(property = "headNumber", column = "head_number"),
            @Result(property = "headCount", column = "head_count"),
            @Result(property = "maintenanceFrequency", column = "maintenance_frequency"),
            @Result(property = "warningDays", column = "warning_days"),
            @Result(property = "useDay", column = "use_day"),
            @Result(property = "count", column = "count"),
            @Result(property = "maintenanceItem", column = "maintenance_item"),
            @Result(property = "maintenanceResult", column = "maintenance_result"),
            @Result(property = "implementer", column = "implementer"),
            @Result(property = "abnormalDescription", column = "abnormal_description")
    })
    @Select("<script>" +
            "SELECT \n" +
            "    ml_latest.id,\n" +
            "    mdc.line, mdc.side, ml_latest.maintenance_date,mdc.id as device_config_id, mdc.machine_number, \n" +
            "    mdc.machine_head, mdc.head_number, ml_latest.head_count, \n" +
            "    mdc.maintenance_frequency, mdc.warning_days,\n" +
            "    DATEDIFF(CURDATE(), ml_latest.maintenance_date) as use_day, \n" +
            "    ml_latest.count, ml_latest.maintenance_item, ml_latest.maintenance_result, \n" +
            "    ml_latest.implementer, ml_latest.abnormal_description\n" +
            "FROM maintenance_device_config mdc\n" +
            "LEFT JOIN (\n" +
            "    SELECT ml.*, lm.count\n" +
            "    FROM maintenance_logdata ml\n" +
            "    INNER JOIN (\n" +
            "        SELECT \n" +
            "            device_config_id,\n" +
            "            MAX(maintenance_date) AS max_latest_date,\n" +
            "            COUNT(*) as count\n" +
            "        FROM maintenance_logdata\n" +
            "        GROUP BY device_config_id\n" +
            "    ) lm ON ml.device_config_id = lm.device_config_id \n" +
            "          AND ml.maintenance_date = lm.max_latest_date\n" +
            ") ml_latest ON mdc.id = ml_latest.device_config_id\n" +
            "<where>\n" +
            "    <if test='line != null'>AND mdc.line = #{line}</if>\n" +
            "    <if test='machineNumber != null and machineNumber != \"\"'>\n" +
            "        AND mdc.machine_number LIKE CONCAT('%', #{machineNumber}, '%')\n" +
            "    </if>\n" +
            "    <if test='headNumber != null and machineNumber != \"\"'>\n" +
            "        AND mdc.head_number = #{headNumber}\n" +
            "    </if>\n" +
            "</where>\n" +
            "ORDER BY mdc.line, mdc.machine_number, mdc.head_number\n" +
            "</script>")
    List<MaintenanceDL> maintenanceDLBySearch(@Param("line") Integer line,@Param("machineNumber") String machineNumber,@Param("headNumber")  String headNumber);


    @Insert("insert into maintenance_logdata(device_config_id,maintenance_date,head_count,maintenance_item,maintenance_result,implementer,abnormal_description) values(" +
            "#{maintenanceDL.deviceConfigId},"+
            "CURDATE(),"+
            "#{maintenanceDL.headCount},"+
            "#{maintenanceDL.maintenanceItem},"+
            "#{maintenanceDL.maintenanceResult},"+
            "#{maintenanceDL.implementer},"+
            "#{maintenanceDL.abnormalDescription}"+
            ")")
    int addMaintenanceLogdata(@Param("maintenanceDL") MaintenanceDL maintenanceDL);

    @Update("UPDATE maintenance_logdata SET " +
            "maintenance_date = #{maintenanceDate}, " +
            "head_count = #{headCount}, " +
            "maintenance_item = #{maintenanceItem}, " +
            "maintenance_result = #{maintenanceResult}, " +
            "implementer = #{implementer}, " +
            "abnormal_description = #{abnormalDescription} " +
            "WHERE id = #{id}")
    int updateMaintenanceLogdata(@Param("id") long id,@Param("maintenanceDate") String maintenanceDate,@Param("headCount") String headCount, @Param("maintenanceItem")String maintenanceItem,@Param("maintenanceResult") String maintenanceResult,@Param("implementer") String implementer,@Param("abnormalDescription") String abnormalDescription);

    @Delete("delete from maintenance_logdata where id=#{id}")
    int deleteMaintenanceLogdata(@Param("id") Long id);

    @ResultMap("maintenanceRecordMap")
    @Select({"<script>",
            "select   ml.id,line, side, maintenance_date,mdc.id as device_config_id, mdc.machine_number, \n" + "               mdc.machine_head, mdc.head_number, head_count, \n" +
             "               mdc.maintenance_frequency, mdc.warning_days,\n" +
             "               maintenance_item, maintenance_result,\n" +
             "               implementer, abnormal_description from maintenance_logdata ml INNER JOIN maintenance_device_config mdc on ml.device_config_id=mdc.id ",
            "<where>",
            "   <if test='line != null'>AND mdc.line = #{line}</if>",
            "   <if test='machineNumber != null and machineNumber != \"\"'>AND mdc.machine_number LIKE CONCAT('%', #{machineNumber}, '%')</if>",
            "   <if test='headNumber != null and headNumber != \"\"'>AND mdc.head_number LIKE CONCAT('%', #{headNumber}, '%')</if>",
            "   <!-- 时间条件：有参数则用参数，无参数则查当月 -->",
            "   <choose>",
            "       <when test='timeStar != null and timeStar != \"\" and timeEnd != null and timeEnd != \"\"'>",
            "           AND ml.maintenance_date BETWEEN #{timeStar} AND #{timeEnd}",
            "       </when>",
            "       <otherwise>",
            "           AND ml.maintenance_date BETWEEN DATE_FORMAT(CURDATE(), '%Y-%m-01') AND LAST_DAY(CURDATE())",
            "       </otherwise>",
            "   </choose>",
            "</where>",
            "</script>"})
    List<MaintenanceDL> queryMaintenanceDLList(@Param("line") Integer line,@Param("machineNumber") String machineNumber, @Param("headNumber")String headNumber,@Param("timeStar") String timeStar,@Param("timeEnd") String timeEnd);
}
